// create the canvas
var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");

canvas.width = 800;
canvas.height = 600;

document.body.appendChild(canvas);

var keysDown = [];
window.addEventListener("keydown", function (e) {
    keysDown[e.keyCode] = true;
}, false);
window.addEventListener("keyup", function (e) {
	if ((e.keyCode == 38 || e.keyCode == 87) && falling == false) 
		{ 
			falling = true;
			YVel = YVel/3;
		}
	if (e.keyCode == 32 && fired == true) 
		{ 
			fired = false;
		}	
    delete keysDown[e.keyCode]; 
}, false);

var update = function (modifier) {
		// Move that bullet!
	var i = bulletTime.length;
	for (var e = 0; e < i; e++) {
		if (bulletTime[e].x < canvas.width + 50 || bulletTime[e].x > canvas.width - 50) {
		if (bulletTime[e].direction == "right") 
			{
					bulletTime[e].x += bulletTime[e].speed * modifier;
					if (bulletTime[e].x > squareEnemy.x && bulletTime[e].x < squareEnemy.x + squareEnemy.x2 && bulletTime[e].y > squareEnemy.y && bulletTime[e].y < squareEnemy.y + squareEnemy.y2)
						{
							squareEnemy.state = false;
							setInterval(function() {squareEnemy.state = true;}, 5000);
						}
			}else
				{
					bulletTime[e].x -= bulletTime[e].speed * modifier;
					if (bulletTime[e].x > squareEnemy.x && bulletTime[e].x < squareEnemy.x + squareEnemy.x2 && bulletTime[e].y > squareEnemy.y && bulletTime[e].y < squareEnemy.y + squareEnemy.y2)
						{
							squareEnemy.state = false;
							setInterval(function() {squareEnemy.state = true;}, 5000);
						}
				}
		}
	}
	
	//Damage to circle!
	if (myCircle.x > squareEnemy.x && myCircle.x < squareEnemy.x + squareEnemy.x2 && myCircle.y > squareEnemy.y && myCircle.y < squareEnemy.y + squareEnemy.y2 && damageImmunity == false && squareEnemy.state == true)
	{
		myCircle.currentHp--;
		damageImmunity = true;
		damageTime = Date.now();
	}
	if (Date.now() > damageTime+500) { damageImmunity = false; }

	//Gravity time!
	YVel = YVel - YAcc * modifier;
	if (YVel < -19 ) { YVel = -19;}
	
	if (squareEnemy.y < 500 && squareEnemy.state == true){
		squareEnemy.y = squareEnemy.y - YVel;
		}
	else		
	{
	squareEnemy.y = 500;
	}
	if (myCircle.y < 525) {
		myCircle.y = myCircle.y - YVel;	
	}
	else
	{
		falling = false;
		myCircle.y = 525;
		YVel = 0;
	}

    if (37 in keysDown || 65 in keysDown) {
        if (myCircle.x > 75) {
            myCircle.x -= myCircle.speed * modifier;
        }
		myCircle.direction = "left";
    } // left
    /* Blocking the shit out of this key
	if (38 in keysDown) {
        if (myCircle.y > 75) {
            myCircle.y -= myCircle.speed * modifier;
        }
    } // up
	*/
    if (39 in keysDown || 68 in keysDown) {
        if (myCircle.x < 725) {
            myCircle.x += myCircle.speed * modifier;
        }
		myCircle.direction = "right";
	} // right
    /* Blocking the shit out of this key
	if (40 in keysDown) {
        if (myCircle.y < 525) {
            myCircle.y += myCircle.speed * modifier;
        }
    } // down
	*/
	// Jumping
	if ((38 in keysDown || 87 in keysDown) && falling == false) {
		if (myCircle.y > 75) 
		{	
			myCircle.y -= myCircle.jumpStrength * modifier	
		}
	else
	{
		YVel = 0;
	}
	} // up or W
	//Shooting
	if (32 in keysDown && fired == false) {
		if (f == 4) {f = 0;}
		bulletTime[0+f] = new circleJerkin(myCircle.x+35, myCircle.y+35, 15, 800);
		bulletTime[0+f].direction = myCircle.direction;
		f++;
		fired = true;
	}

};

function draw() {
    // fill canvas so we have something to look at for now
    ctx.fillStyle = "black";
    ctx.fillRect(0, 0, canvas.width, canvas.height);
			
    ctx.beginPath();
    ctx.arc(myCircle.x, myCircle.y, myCircle.radius, 0, 2 * Math.PI, false);
    ctx.fillStyle = 'red';
    ctx.fill();
    ctx.lineWidth = 5;
    ctx.strokeStyle = 'orange';
    ctx.stroke();
	
	if (squareEnemy.state == true) 
	{
	ctx.fillStyle = "yellow";
	ctx.lineWidth = 2;
	ctx.fillRect(squareEnemy.x, squareEnemy.y, squareEnemy.x2, squareEnemy.y2);
	}
	
	
	var i = bulletTime.length;
	for (var e = 0; e < i; e++) {
		if (bulletTime[e].x < canvas.width + 50 || bulletTime[e].x > canvas.width - 50) {
		ctx.beginPath();
		ctx.arc(bulletTime[e].x, bulletTime[e].y, bulletTime[e].radius, 0, 2 * Math.PI, false);
		ctx.fillStyle = 'white';
		ctx.fill();
		ctx.lineWidth = 2.5;
		ctx.strokeStyle = '#003300';
		ctx.stroke();
		}
	}
	
	gameUi();

};

function squareNazi(x, y, x2, y2, speed) {
	this.x = x;
	this.y = y;
	this.x2 = x2;
	this.y2 = y2;
	this.speed = speed;
	this.state;
};

function circleJerkin(x, y, radius, speed) {
    this.x = x;
    this.y = y;
    this.radius = radius;
    this.speed = speed;
	this.direction;
	this.jumpStrength;
	this.currentHp;
	this.maxHp;
};

function debugOutput() {
	ctx.fillStyle = "white";
    ctx.font = "30px Arial";
	ctx.fillText("X: " + myCircle.x.toString(), 670, 30);
	ctx.fillText("Y: " + myCircle.y.toString(), 670, 65);
	ctx.fillText("Vel: " + YVel.toString(), 670, 100);
	ctx.fillText("B #: " + bulletTime.length.toString(), 670, 135);
};

function gameUi() {
	var spacing = 5;
	var spacing2 = 5;
	for (var i = 0; i < myCircle.maxHp; i++) {
		hpBar2[i] = new squareNazi(18,18,14,9,256);
		if (i > 9) { hpBar2[i].x = 41; }
		if (i == 10) { spacing2 = 5;}
		ctx.fillStyle = "white";
		ctx.fillRect(hpBar2[i].x, hpBar2[i].y+spacing2, hpBar2[i].x2, hpBar2[i].y2);
		spacing2 = spacing2 + 14;
	}
	for (var e = 0; e < myCircle.currentHp; e++) {
		hpBar[e] = new squareNazi(20,20,10,5,256);
		if (e > 9) { hpBar[e].x = 43; }
		if (e == 10) { spacing = 5;}
		ctx.fillStyle = "#00FF00";
		ctx.fillRect(hpBar[e].x, hpBar[e].y+spacing, hpBar[e].x2, hpBar[e].y2);
		spacing = spacing + 14;
	}
};

var main = function () {
    var now = Date.now();
    var delta = now - then;
	
	update(delta / 1000);
    draw();
	debugOutput();
    then = now;

};

var then = Date.now();
var myCircle = new circleJerkin(75, 75, 70, 256);
var squareEnemy = new squareNazi(450,450,100,100,256);
var damageImmunity = false;
var damageTime = 0;
var YVel = 0;
var YAcc = 16.5;
var falling = false;
var fired = false;
var hpBar = new Array();
var hpBar2 = new Array();
myCircle.direction = "right";
myCircle.currentHp = 20;
myCircle.maxHp = 20;
myCircle.jumpStrength = 1000;
squareEnemy.state = true;
var bulletTime = new Array();
var f = 0;
setInterval(main, 1);